package com.cnyz.controller;

import com.cnyz.base.ResponseResult;
import com.cnyz.dto.NoticeDto;
import com.cnyz.entity.Notice;
import com.cnyz.service.NoticeService;
import com.cnyz.vo.NoticeVo;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/notice")
@ApiModel(description = "通知接口")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PostMapping("/add")
    public ResponseResult addNotice(@RequestBody NoticeDto noticeDto) {
        List<Notice> notice = new ArrayList<>();
        for(Long i: noticeDto.getReceivers()){
            Notice j = new Notice();
            BeanUtils.copyProperties(noticeDto,j);
            j.setReceiverID(i);
            notice.add(j);
        }
        noticeService.addSomeNotices(notice);
        return ResponseResult.success("发送通知成功");
    }

    @PostMapping("/update")
    public ResponseResult updateNotice(@RequestBody Notice notice) {
        noticeService.updateNotice(notice);
        return ResponseResult.success("更新通知成功");
    }

    @DeleteMapping("/delete")
    public ResponseResult deleteNotice(@RequestParam Long id) {
        noticeService.deleteNotice(id);
        return ResponseResult.success("删除课程成功");
    }

    @GetMapping("/id")
    public ResponseResult<NoticeVo> getNoticeByID(@RequestParam Long id) {
        return ResponseResult.success(noticeService.getNoticeByID(id));
    }

    @GetMapping("/userid")
    public ResponseResult<List<NoticeVo>> getNoticeByUserId(@RequestParam Long userID) {
        return ResponseResult.success(noticeService.getNoticesByUserID(userID));
    }
}
